home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Applications 2004 May / SGI IRIX 6.5 Applications 2004 May.iso / dist / java3d.idb / usr / demos / java / j3d / programs / examples / GearTest / GearTest.java.z / GearTest.java
Encoding:
Java Source  |  2003-08-08  |  6.6 KB  |  202 lines

  1. /*
  2.  *    @(#)GearTest.java 1.17 02/10/21 13:40:16
  3.  *
  4.  * Copyright (c) 1996-2002 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  *
  10.  * - Redistributions of source code must retain the above copyright
  11.  *   notice, this list of conditions and the following disclaimer.
  12.  *
  13.  * - Redistribution in binary form must reproduce the above copyright
  14.  *   notice, this list of conditions and the following disclaimer in
  15.  *   the documentation and/or other materials provided with the
  16.  *   distribution.
  17.  *
  18.  * Neither the name of Sun Microsystems, Inc. or the names of
  19.  * contributors may be used to endorse or promote products derived
  20.  * from this software without specific prior written permission.
  21.  *
  22.  * This software is provided "AS IS," without a warranty of any
  23.  * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
  24.  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
  25.  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
  26.  * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
  27.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  28.  * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
  29.  * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
  30.  * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
  31.  * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
  32.  * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
  33.  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  34.  *
  35.  * You acknowledge that Software is not designed,licensed or intended
  36.  * for use in the design, construction, operation or maintenance of
  37.  * any nuclear facility.
  38.  */
  39.  
  40. import java.applet.Applet;
  41. import java.awt.*;
  42. import java.awt.event.*;
  43. import com.sun.j3d.utils.applet.MainFrame;
  44. import com.sun.j3d.utils.universe.*;
  45. import javax.media.j3d.*;
  46. import javax.vecmath.*;
  47.  
  48. public class GearTest extends Applet {
  49.  
  50.     static final int defaultToothCount = 24;
  51.  
  52.     private int toothCount;
  53.  
  54.     private SimpleUniverse u = null;
  55.  
  56.     public BranchGroup createSceneGraph(int toothCount) {
  57.     // Create the root of the branch graph
  58.     BranchGroup objRoot = new BranchGroup();
  59.  
  60.         // Create a Transformgroup to scale all objects so they
  61.         // appear in the scene.
  62.         TransformGroup objScale = new TransformGroup();
  63.         Transform3D t3d = new Transform3D();
  64.         t3d.setScale(0.4);
  65.         objScale.setTransform(t3d);
  66.         objRoot.addChild(objScale);
  67.  
  68.     // Create a bounds for the background and lights
  69.     BoundingSphere bounds =
  70.         new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
  71.  
  72.     // Set up the background
  73.     Color3f bgColor = new Color3f(0.05f, 0.05f, 0.2f);
  74.     Background bgNode = new Background(bgColor);
  75.     bgNode.setApplicationBounds(bounds);
  76.     objScale.addChild(bgNode);
  77.  
  78.     // Set up the global lights
  79.     Color3f light1Color = new Color3f(1.0f, 1.0f, 0.9f);
  80.     Vector3f light1Direction  = new Vector3f(4.0f, -7.0f, -12.0f);
  81.     Color3f light2Color = new Color3f(0.3f, 0.3f, 0.4f);
  82.     Vector3f light2Direction  = new Vector3f(-6.0f, -2.0f, -1.0f);
  83.     Color3f ambientColor = new Color3f(0.1f, 0.1f, 0.1f);
  84.  
  85.     AmbientLight ambientLightNode = new AmbientLight(ambientColor);
  86.     ambientLightNode.setInfluencingBounds(bounds);
  87.     objScale.addChild(ambientLightNode);
  88.  
  89.     DirectionalLight light1
  90.         = new DirectionalLight(light1Color, light1Direction);
  91.     light1.setInfluencingBounds(bounds);
  92.     objScale.addChild(light1);
  93.  
  94.     DirectionalLight light2
  95.         = new DirectionalLight(light2Color, light2Direction);
  96.     light2.setInfluencingBounds(bounds);
  97.     objScale.addChild(light2);
  98.  
  99.     // Create the transform group node and initialize it to the
  100.     // identity.  Enable the TRANSFORM_WRITE capability so that
  101.     // our behavior code can modify it at runtime.  Add it to the
  102.     // root of the subgraph.
  103.     TransformGroup objTrans = new TransformGroup();
  104.     objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  105.     objScale.addChild(objTrans);
  106.  
  107.     // Create an Appearance.
  108.     Appearance look = new Appearance();
  109.     Color3f objColor = new Color3f(0.5f, 0.5f, 0.6f);
  110.     Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
  111.     Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
  112.     look.setMaterial(new Material(objColor, black, objColor, white, 100.0f));
  113.  
  114.     // Create a gear, add it to the scene graph.
  115.     //    SpurGear gear = new SpurGear(toothCount, 1.0f, 0.2f,
  116.     SpurGear gear = new SpurGearThinBody(toothCount, 1.0f, 0.2f,
  117.                      0.05f, 0.05f, 0.3f, 0.28f, look);
  118.     objTrans.addChild(gear);
  119.  
  120.     // Create a new Behavior object that will rotate the object and
  121.     // add it into the scene graph.
  122.     Transform3D yAxis = new Transform3D();
  123.     Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE,
  124.                     0, 0,
  125.                     8000, 0, 0,
  126.                     0, 0, 0);
  127.  
  128.     RotationInterpolator rotator =
  129.         new RotationInterpolator(rotationAlpha, objTrans, yAxis,
  130.                      0.0f, (float) Math.PI*2.0f);
  131.     rotator.setSchedulingBounds(bounds);
  132.     objTrans.addChild(rotator);
  133.  
  134.         // Have Java 3D perform optimizations on this scene graph.
  135.         objRoot.compile();
  136.  
  137.     return objRoot;
  138.     }
  139.  
  140.     public GearTest() {
  141.     this(defaultToothCount);
  142.     }
  143.  
  144.     public GearTest(int toothCount) {
  145.     this.toothCount = toothCount;
  146.     }
  147.  
  148.     public void init() {
  149.     setLayout(new BorderLayout());
  150.     GraphicsConfiguration config =
  151.         SimpleUniverse.getPreferredConfiguration();
  152.  
  153.         Canvas3D c = new Canvas3D(config);
  154.     add("Center", c);
  155.  
  156.     // Create a simple scene and attach it to the virtual universe
  157.     BranchGroup scene = createSceneGraph(toothCount);
  158.     u = new SimpleUniverse(c);
  159.  
  160.         // This will move the ViewPlatform back a bit so the
  161.         // objects in the scene can be viewed.
  162.         u.getViewingPlatform().setNominalViewingTransform();
  163.  
  164.     u.addBranchGraph(scene);
  165.     }
  166.  
  167.     public void destroy() {
  168.     u.cleanup();
  169.     }
  170.  
  171.     //
  172.     // The following allows GearTest to be run as an application
  173.     // as well as an applet
  174.     //
  175.     public static void main(String[] args) {
  176.     int value;
  177.     
  178.     if (args.length > 1) {
  179.         System.out.println("Usage: java GearTest [#teeth]");
  180.         System.exit(0);
  181.     } else if (args.length == 0) {    
  182.         new MainFrame(new GearTest(), 700, 700);
  183.     } else
  184.         {
  185.         try {
  186.             value = Integer.parseInt(args[0]);
  187.         } catch (NumberFormatException e) {
  188.             System.out.println("Illegal integer specified");
  189.             System.out.println("Usage: java GearTest [#teeth]");
  190.             value = 0;
  191.             System.exit(0);
  192.         }
  193.         if (value <= 0) {
  194.             System.out.println("Integer must be positive (> 0)");
  195.             System.out.println("Usage: java GearBox [#teeth]");
  196.             System.exit(0);
  197.         }
  198.         new MainFrame(new GearTest(value), 700, 700);
  199.         }
  200.     }
  201. }
  202.